// RedJey Graphics User Interface Library for J2ME Platform.
// Copyright (C) 2008 Yevgen Krapiva
//
// This library is free software; you can redistribute
// it and/or modify it under the terms of the
// GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0
// of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

package com.incrediblesoft.redjey.borders;

/**
 * @author Yevgen Krapiva<br>
 *         Homepage: http://code.google.com/p/redjey/<br>
 *         Feedback: ykrapiva@gmail.com<br>
 *         Copyright (C) 2008 Yevgen Krapiva<br>
 */

import com.incrediblesoft.redjey.borders.AbstractBorder;

import javax.microedition.lcdui.Graphics;
import java.util.Hashtable;

public class SimpleBorder extends AbstractBorder
{
	private int color;
    private int borderWidth;
    private int arcWidth, arcHeight;
    private int style;

    public SimpleBorder( int borderWidth, int color)
    {
        this(borderWidth, color, 0, 0, Graphics.SOLID);
    }

    public SimpleBorder( int borderWidth, int color, int style)
    {
        this(borderWidth, color, 0, 0, style);
    }

    public SimpleBorder( int borderWidth, int color, int arcWidth, int arcHeight)
    {
        this(borderWidth, color, arcWidth, arcHeight, Graphics.SOLID);
    }
    
    public SimpleBorder( int borderWidth, int color, int arcWidth, int arcHeight, int style)
    {
        super();
        this.color = color;
        this.borderWidth = borderWidth;
        this.arcWidth = arcWidth;
        this.arcHeight = arcHeight;
        this.style = style;
    }

    public SimpleBorder(Hashtable styleDesc)
    {
        super(styleDesc);
        borderWidth = 1;
        style = Graphics.SOLID;

        String str;
        str = (String) styleDesc.get("color");
        if (str != null) color = (int) Long.parseLong(str, 16);
        str = (String) styleDesc.get("width");
        if (str != null) borderWidth  = Integer.parseInt(str);
        str = (String) styleDesc.get("arcWidth");
        if (str != null) arcWidth  = Integer.parseInt(str);
        str = (String) styleDesc.get("arcHeight");
        if (str != null) arcHeight  = Integer.parseInt(str);
        str = (String) styleDesc.get("style");
        if (str != null && str.equals("dotted"))
            style = Graphics.DOTTED;
    }

    public int getLeftBorderWidth()
    {
        return borderWidth;
    }

    public int getTopBorderWidth()
    {
        return borderWidth;
    }

    public int getRightBorderWidth()
    {
        return borderWidth;
    }

    public int getBottomBorderWidth()
    {
        return borderWidth;
    }

    public int getMinWidth()
    {
        return borderWidth * 2;
    }

    public int getMinHeight()
    {
        return borderWidth * 2;
    }

    public int getArcWidth()
    {
        return arcWidth;
    }

    public void setArcWidth(int arcWidth)
    {
        this.arcWidth = arcWidth;
    }

    public int getArcHeight()
    {
        return arcHeight;
    }

    public void setArcHeight(int arcHeight)
    {
        this.arcHeight = arcHeight;
    }

    public void paint(Graphics g, int x, int y, int width, int height)
    {
        g.setColor( this.color );
        g.setStrokeStyle( this.style );
        for (int i=0; i<borderWidth; i++)
        {
            if (arcWidth > 0 || arcHeight > 0)
                g.drawRoundRect(x+i, y+i, width-1-(i << 1), height-1-(i << 1), arcWidth, arcHeight);
            else
                g.drawRect(x+i, y+i, width-1-(i << 1), height-1-(i << 1));
        }

    }

    public AbstractBorder clone()
    {
        SimpleBorder newBorder = new SimpleBorder(borderWidth, color, arcWidth, arcHeight, style);
        return newBorder;
    }
}
